package com.hare.system_server.controller;


import com.hare.auth.base.BaseLoginUser;
import com.hare.auth.utils.SecurityUtils;
import com.hare.common.result.R;
import com.hare.system_api.dto.SysUserDTO;
import com.hare.system_api.vo.RoutesVO;
import com.hare.system_api.vo.SysMenuVO;
import com.hare.system_server.serice.SysMenuService;
import com.hare.system_server.serice.SysUserLoginService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: Hare
 * @description: 登录Api
 * @author: Hare
 * @create: 2022-06-22 13:10
 * @Version 6.0.1
 **/
@RestController
@RequestMapping("/auth")
//@Api(tags = "登录相关Api")
public class SysUserLoginController {


    @Autowired
    SysUserLoginService sysUserLoginService;

    @Autowired
    SysMenuService sysMenuService;


    /**
     * 后台管理=> 登录
     *
     * @param loginUserDTO 用户账号密码
     * @return token
     */
    //@ApiOperation("登录")
    @PostMapping("/login")
    public R<Map<String, Object>> adminLogin(@RequestBody SysUserDTO loginUserDTO) {
        return R.success(sysUserLoginService.adminLogin(loginUserDTO));

    }


    /**
     * 后台管理=> 退出登录
     *
     * @param request
     * @return
     */
    //@ApiOperation("退出登录")
    @PostMapping("/loginOut")
    public R loginOut(HttpServletRequest request) {
        sysUserLoginService.loginOut(request);
        return R.successOk();
    }


    /**
     * 后台管理=> 获取用户信息
     *
     * @return 用户权限及信息
     */
    //@ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public R<Map<String, Object>> getUserInfo() {
        BaseLoginUser admin = SecurityUtils.getAdmin();
        Map<String, Object> userMap = new HashMap<>();
        Set<String> preSetInfo = sysMenuService.selectPreGetByUserId(admin.getId());
        // 用户信息
        userMap.put("user", admin);
        // 权限信息
        userMap.put("permissions", preSetInfo);
        return R.success(userMap);
    }


    /**
     * 后台管理=> 获取路由(菜单信息)
     *
     * @return
     */
    //@ApiOperation("获取菜单信息")
    @GetMapping("/getRoutes")
    public R<List<RoutesVO>> getRoures() {
        BaseLoginUser admin = SecurityUtils.getAdmin();
        List<SysMenuVO> menuVOList = sysMenuService.selectMenuTreeByUserId(admin.getId());
        return R.success(sysMenuService.getRoutesTree(menuVOList));
    }
}
